package com.personal.common.utils;

import java.math.BigInteger;
import java.util.BitSet;

import com.greenpineyu.fel.FelEngine;
import com.greenpineyu.fel.FelEngineImpl;

public class NumUtil {
	/***
	 * 计算表达式
	 * @param expression
	 * @return
	 */
	public static Object expressionToNumber(String expression){
		try{
			FelEngine fel = new FelEngineImpl(); 
			return fel.eval(expression);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	
	public static double getCTorPT(String StrCTorPT){
		double ctOrPt = 1;
		if(StrCTorPT != null &&!StringUtil.isEmptyString(StrCTorPT.trim())){
			if(StrCTorPT.contains("/")){
				String[] tmp = StrCTorPT.split("/");
				double one = Double.valueOf(tmp[0]);
				double second = Double.valueOf(tmp[1]);
				return one/second;
			}
		}
		return ctOrPt ;
		
	}
	
	/***
	 * bitSet 转换成bigInt
	 * @param bitSet
	 * @return
	 */
	public static BigInteger bitSet2bigInt(BitSet bitSet) {
        byte[] bytes = new byte[bitSet.size() / 8];
        for (int i = 0; i < bitSet.size(); i++) {
            int index =bitSet.size()/8 - i / 8 -1;
            int offset = i % 8;
            bytes[index] |= (bitSet.get(i) ? 1 : 0) << offset;
        }
        return new BigInteger(bytes);
	}
	

}
